package roboticsfmw.sample.hardware;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import roboticsfmw.core.HardwareException;
import roboticsfmw.core.HardwareProxy;


public class UserIOHardwareProxy implements HardwareProxy {

	private Logger log;
	private JoystickCanvas canvas;
	private JFrame frame;
	private JTextField commandField;
	public String lastCommand;
	
	@Override
	public void init() {
		log = Logger.getLogger(UserIOHardwareProxy.class);
		frame = new JFrame();
		frame.setLayout(new BorderLayout());
		frame.setSize(500,300);
		frame.setName("Teste de JFrame");
		this.canvas = new JoystickCanvas();
		frame.add(canvas, BorderLayout.CENTER);
		Container commandContainer = new Container();
		commandContainer.setLayout(new FlowLayout());
		frame.add(commandContainer, BorderLayout.SOUTH);
		JLabel commandLabel = new JLabel("Command>");
		commandField = new JTextField();
		commandField.setColumns(20);
		JButton commandButton = new JButton(" Send ");
		commandContainer.add(commandLabel);
		commandContainer.add(commandField);
		commandContainer.add(commandButton);
		lastCommand = "";
		commandButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (!"".equals(commandField.getText())) {
					lastCommand=commandField.getText();
				}
				commandField.setText("");
			}
			
		});
		
		frame.setVisible(true);
	}	
	
	@Override
	public void shutdown() throws HardwareException {
		log.info("Arm Hardware Shutdonw!");
		frame.dispose();
	}

	public void moveArmX(int value) {
		this.canvas.setX(value);
	}
	
	public void moveArmY(int value) {
		this.canvas.setY(value);
	}
	
	public void moveSubArmX(int value) {
		log.warn("Sub Arm moving Axis X: "+value);				
	}
	
	public void moveHand(int value) {
		if (value == 1){
			log.warn("Arm Hand Close!");		
		} else {
			log.warn("Arm Hand Open!");		
		}
	}

}
